import { TextEncoder } from './TextEncoder.js';
import { TextDecoder } from './TextDecoder.js';

// encoding
function encodedDataTwo(){
    const inputString = 'ABCDEFGHIJKabcdefj123456+/AA==!@';
    let encodedDataResult = new Uint8Array([
        65, 66, 67, 68, 69,  70,  71,  72,  73,
        74, 75, 97, 98, 99, 100, 101, 102, 106,
        49, 50, 51, 52, 53,  54,  43,  47,  65,
        65, 61, 61, 33, 64
    ]) 

    let startTime = Date.now();
    let encodedData;
    for (let i = 0; i < 10000; i++) {
        encodedData = new TextEncoder('utf-8').encode(inputString);
    }
    let endTime = Date.now();
   
    if(arraysEqual(encodedData, encodedDataResult) == false){
        throw new Error('zxing-text-encodingDemo_encode_0100')
    }
    print(`zxing-text-encodingDemo_encode_0100: ${endTime - startTime} ms`)
}


// decode
function decodedArrayTwo(){
    let encodedData = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])

    let startTime = Date.now();
    let decodedArray;
    for (let i = 0; i < 10000; i++) {
        decodedArray = new TextDecoder('utf-8').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray !== 'Hello World'){
        throw new Error('zxing-text-encodingDemo_encode_0100')
    }
    print(`zxing-text-encodingDemo_decode_0100: ${endTime - startTime} ms`)
}

// Judgment method
function arraysEqual(a, b) {
    if (a.length !== b.length) return false;
    for (let i = 0; i < a.length; i++) {
      if (a[i] !== b[i]) return false;
    }
    return true;
}

function Switch() {
    encodedDataTwo()
    decodedArrayTwo()
}
Switch()